package com.hserver.docker.controller;

import com.github.dockerjava.api.model.Service;
import com.github.dockerjava.api.model.Swarm;
import com.github.dockerjava.api.model.SwarmNode;
import com.github.dockerjava.api.model.SwarmNodeAvailability;
import com.hserver.docker.bean.DockerUrl;
import com.hserver.docker.config.ConstConfig;
import com.hserver.docker.service.DockerUrlService;
import com.hserver.docker.service.SwarmService;
import top.hserver.core.interfaces.HttpResponse;
import top.hserver.core.ioc.annotation.Autowired;
import top.hserver.core.ioc.annotation.Controller;
import top.hserver.core.ioc.annotation.GET;
import top.hserver.core.ioc.annotation.POST;
import top.hserver.core.server.util.JsonResult;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("/swarm/")
public class SwarmController {

    @Autowired
    private DockerUrlService dockerUrlService;

    @Autowired
    private SwarmService swarmService;

    @GET("index")
    public void index(HttpResponse response) {
        Swarm swarm = swarmService.checkSwarm();
        Map<String, Object> map = new HashMap<>(1);
        map.put("swarm", swarm);
        response.sendTemplate("/swarm/index.ftl", map);
    }

    @GET("list")
    public void list(HttpResponse response) {
        List<SwarmNode> swarmNodes = swarmService.listSwarm();
        Map<String, Object> map = new HashMap<>(1);
        map.put("nodeList", swarmNodes);
        response.sendTemplate("/swarm/list.ftl", map);
    }

    @GET("serviceList")
    public void serviceList(HttpResponse response) {
        List<Service> swarmNodes = swarmService.listSwarmService();
        Map<String, Object> map = new HashMap<>(1);
        map.put("serviceList", swarmNodes);
        response.sendTemplate("/swarm/serviceList.ftl", map);
    }

    @GET("init")
    public void init(HttpResponse response) {
        response.sendTemplate("/swarm/init.ftl");
    }

    @GET("addNode")
    public void addNode(HttpResponse response) {
        Map<String, Object> map = new HashMap<>(2);
        String host = "";
        try {
            if (ConstConfig.DOCKER_URL.startsWith("tcp")) {
                URL url = new URL(ConstConfig.DOCKER_URL.replace("tcp", "http"));
                host = url.getHost();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        map.put("host", host);
        map.put("dockerList", dockerUrlService.list());
        response.sendTemplate("/swarm/addNode.ftl", map);
    }

    @POST("initSwarm")
    public JsonResult initSwarm(String address) {
        String s = swarmService.initSwarm(address);
        if (s == null) {
            return JsonResult.ok();
        }
        return JsonResult.error(s);
    }

    @POST("addSwarm")
    public JsonResult addSwarm(String address1, String address2) {
        String s = swarmService.addSwarm(address1, address2);
        if (s == null) {
            return JsonResult.ok();
        }
        return JsonResult.error(s);
    }

    @POST("updateSwarmNode")
    public JsonResult updateSwarmNode(String id, Integer type) {
        if (type != null) {
            SwarmNodeAvailability tmp = null;
            switch (type) {
                case 1:
                    tmp = SwarmNodeAvailability.ACTIVE;
                    break;
                case 2:
                    tmp = SwarmNodeAvailability.PAUSE;
                    break;
                case 3:
                    tmp = SwarmNodeAvailability.DRAIN;
                    break;
            }
            swarmService.updateSwarmNode(id, tmp);
        }
        return JsonResult.ok();
    }

    @GET("disbandCluster")
    public void disbandCluster(HttpResponse response) {
        swarmService.disbandCluster();
        index(response);
    }

    @POST("removeNode")
    public JsonResult removeNode(String url) {
        try {
            swarmService.removeNode(url);
        }catch (Exception e){
            return JsonResult.ok(e.getMessage());
        }
        return JsonResult.ok();
    }

    @GET("removeService")
    public void removeService(String service, HttpResponse response) {
        swarmService.removeService(service);
        serviceList(response);
    }

    @GET("removeNode")
    public void removeNode(HttpResponse response) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("dockerList", dockerUrlService.list());
        response.sendTemplate("/swarm/removeNode.ftl", map);
    }

}
